import javax.swing.*;
import java.awt.*;

public class BallGame5 extends JFrame {  //继承JFrame
    //添加图片
    Image ball = Toolkit.getDefaultToolkit().getImage("images/ball.png");
    Image desk = Toolkit.getDefaultToolkit().getImage("images/desk.png");

    //定义小球初始位置
    double x=100;
    double y = 100;
    double degree = 3.14/3;

    //画窗口，加载小球与桌面
    public void paint (Graphics g){
        System.out.println("窗口被画一次");
        g.drawImage(desk,0,0,null);
        g.drawImage(ball,(int)x,(int)y,null);
        //根据角度degree改变小球坐标
        x = x+10*Math.cos(degree);
        y = y+10*Math.sin(degree);
        //边界检测
        if(y>500-40-30||y<40+40){
            degree =-degree;
        }
        if(x<40||x>856-40-30){
            degree = 3.14-degree;
        }
    }
    //窗口加载
    void launchFrame(){
        setSize(856,500);
        setLocation(50,50);
        setVisible(true);
        //重画窗口
        while (true){
            repaint();
            try {
                Thread.sleep(40);
            }catch (Exception e){
                e.printStackTrace();
            }

        }
    }

    public static void main(String[] args) {
        System.out.println("桌球游戏");
        BallGame5 game5 = new BallGame5();
        game5.launchFrame();
    }
}
